package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.NearPeopleAdapter;
import com.example.capsule_chat0322.bean.PositionBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.NearPeopleItemOnClickInterface;
import com.example.capsule_chat0322.request.PositionRequest;
import com.example.capsule_chat0322.request.UserInfoRequest;
import com.example.capsule_chat0322.utils.AlterUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/15 23:09
 */
public class NearPeopleActivity extends AppCompatActivity {

    private List<PositionBean> positionBeanList;
    private RecyclerView near_people_recycler_view;
    private NearPeopleAdapter nearPeopleAdapter;
    private EditText search_text;
    private TextView refresh_near_people;
    private TextView search_near_people;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.near_people);
        initView();//初始化UI控件
    }

    private void initView() {

        near_people_recycler_view = findViewById(R.id.near_people_recycler_view);
        search_text = findViewById(R.id.search_text);
        refresh_near_people = findViewById(R.id.refresh_near_people);
        search_near_people = findViewById(R.id.search_near_people);

        PositionBean positionBean = new PositionBean();
        positionBean.setField(GlobalAssistMap.getObject("field").toString());
        positionBean.setUserId(GlobalAssistMap.getObject("userId").toString());

        getNearPeople(positionBean);

        refresh_near_people.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getNearPeople(positionBean);
            }
        });

        search_near_people.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchId = search_text.getText().toString();
                if (searchId.length() > 11 || !judgeIsDigit(searchId)) {
                    AlterUtils.alertByToastInMainThread(NearPeopleActivity.this, "请输入正规Id");
                    search_text.setText("");
                    return;
                }

                PositionBean positionBean = new PositionBean();
                positionBean.setField(GlobalAssistMap.getObject("field").toString());
                positionBean.setUserId(GlobalAssistMap.getObject("userId").toString());
                UserInfoBean userInfoBean = (UserInfoBean) GlobalAssistMap.getObject(searchId + "Info");
                if (userInfoBean != null) {
                    positionBean.setUserName(userInfoBean.getNickname());
                    positionBean.setSex(userInfoBean.getSex());
                }
                positionBean.setOtherId(searchId);
                PositionRequest.getDistance(positionBean, new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(@NonNull Message message) {
                        PositionBean positionBean = (PositionBean) message.obj;
                        positionBeanList = new ArrayList<>();
                        if (positionBean.getDistance() != null) {
                            positionBeanList.add(positionBean);
                        }
                        LinearLayoutManager layoutManager = new LinearLayoutManager(NearPeopleActivity.this);
                        near_people_recycler_view.setLayoutManager(layoutManager);
                        nearPeopleAdapter = new NearPeopleAdapter(positionBeanList);
                        adapterAddListener(nearPeopleAdapter);
                        near_people_recycler_view.setAdapter(nearPeopleAdapter);
                        return true;
                    }
                }));


            }
        });

    }

    private void getNearPeople(PositionBean positionBean) {
        PositionRequest.getNearPeople(positionBean, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                positionBeanList = (List<PositionBean>) message.obj;
                LinearLayoutManager layoutManager = new LinearLayoutManager(NearPeopleActivity.this);
                near_people_recycler_view.setLayoutManager(layoutManager);
                nearPeopleAdapter = new NearPeopleAdapter(positionBeanList);
                adapterAddListener(nearPeopleAdapter);
                near_people_recycler_view.setAdapter(nearPeopleAdapter);
                return true;
            }
        }));
    }


    public void adapterAddListener(NearPeopleAdapter nearPeopleAdapter) {

        if (nearPeopleAdapter == null) {
            return;
        }

        nearPeopleAdapter.setFunctionItemOnClickInterface(new NearPeopleItemOnClickInterface() {

            @Override
            public void onItemClick(RecyclerView parent, View view, String senderId) {

                final UserInfoBean[] userInfoBean = {(UserInfoBean) GlobalAssistMap.getObject(senderId + "Info")};

                if (userInfoBean[0] == null) {
                    UserInfoRequest.doGetUserInfoRequest(new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            userInfoBean[0] = (UserInfoBean) message.obj;
                            jump(senderId, userInfoBean[0]);
                            return true;
                        }
                    }), senderId);
                } else {
                    jump(senderId, userInfoBean[0]);
                }


            }
        });
    }

    private void jump(String senderId, UserInfoBean userInfoBean) {
        Intent intent = new Intent();
        intent.putExtra("senderId", Integer.parseInt(senderId));
        intent.putExtra("senderAvatar", userInfoBean.getAvatarUrl());
        intent.putExtra("senderSign", userInfoBean.getSign());
        intent.setClass(NearPeopleActivity.this, UserInfoActivity.class);
        startActivity(intent);
    }

    private boolean judgeIsDigit(String number) {

        for (int i = 0; i < number.length(); i++) {
            if (!Character.isDigit(number.charAt(i))) {
                return false;
            }
        }
        return true;

    }


}
